#include <QDebug>
#include <time.h>
#include <QTime>
#include <QPluginLoader>

#include "unionhmidll_global.h"
#include "WorkThreadManage/hmithreadmanage.h"
#include "ui_mainwindow.h"

HmiThread::HmiThread(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
    this->pTecControllerHMI = nullptr;
    this->pRobControllerHMI = nullptr;
    this->ui->setupUi(this);
    this->MainWindowInit();
    this->SignalSlotConnect();
    this->HmiPluginInstanceHMILoading();
    this->RunLed = true;
}

HmiThread::~HmiThread(){
    delete pTecControllerHMI;
    delete pRobControllerHMI;
}

short HmiThread :: MainWindowShow(){
    this->show();
    return 1;
}

short HmiThread :: MainWindowHide(){
    this->hide();
    return 1;
}

short  HmiThread :: MainWindowInit(){
    this->pTecControllerHMI = nullptr;
    this->pRobControllerHMI = nullptr;

    this->ControllerHmiPage = 0;

    this->mMessageNumber.ErrorMessageNumber = 0;
    this->mMessageNumber.WarningMessageNumber = 0;
    this->mMessageNumber.NormalMessageNumber = 0;

    this->ui->progressBar->setValue(100);
    return 1;
}

short HmiThread::HmiPluginInstanceHMILoading(){
    if(this->pTecControllerHMI == nullptr){
      QPluginLoader pluginLoader(QCoreApplication::applicationDirPath() + "/" + "UnionTecControllerHMIDLL.dll");
        this->pTecControllerHMI = qobject_cast<TecControllerHMI*>(pluginLoader.instance());
        if(this->pTecControllerHMI != nullptr){
            this->ui->Main->insertWidget(1,(pTecControllerHMI->RegisiterUi()));
            this->ControllerHmiPage = 0;
            this->ui->Main->setCurrentIndex(ControllerHmiPage);
            this->ui->TecModeManualSelected->setEnabled(true);
            this->ui->TecControllerSelected->setEnabled(true);
        }
        else{
            this->ui->TecModeManualSelected->setEnabled(false);
            this->ui->TecControllerSelected->setEnabled(false);
        }
    }

    if(this->pRobControllerHMI == nullptr){
      QPluginLoader pluginLoader(QCoreApplication::applicationDirPath() + "/" + "UnionRobControllerHMIDLL.dll");
        this->pRobControllerHMI = qobject_cast<RobControllerHMI*>(pluginLoader.instance());
        if(this->pRobControllerHMI != nullptr){
            this->ui->Main->insertWidget(2,(pRobControllerHMI->RegisiterUi()));
            this->ControllerHmiPage = 0;
            this->ui->Main->setCurrentIndex(ControllerHmiPage);
            this->ui->RobModeM1Selected->setEnabled(true);
            this->ui->RobModeM2Selected->setEnabled(true);
            this->ui->RobModeAUTOSelected->setEnabled(true);
            this->ui->RobModeREMOTESelected->setEnabled(true);
            this->ui->RobControllerSelected->setEnabled(true);
        }
        else{
            this->ui->RobModeM1Selected->setEnabled(false);
            this->ui->RobModeM2Selected->setEnabled(false);
            this->ui->RobModeAUTOSelected->setEnabled(false);
            this->ui->RobModeREMOTESelected->setEnabled(false);
            this->ui->RobControllerSelected->setEnabled(false);
        }
    }
    return 1;
}

short HmiThread :: WriteMessageBox(MessageType Type, const QString Data){
    QString AllData;
    QTime time = QTime::currentTime();
    switch (Type) {
    case MessageType::Message: //消息栏打印普通信息
        AllData = time.toString("hh:mm:ss") + ": <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + Data;
        NormalMessage.push_back(AllData);
        mMessageNumber.NormalMessageNumber = mMessageNumber.NormalMessageNumber + 1;
        this->ui->NormalMessageNumber->setNum(mMessageNumber.NormalMessageNumber);
        break;
    case MessageType::Warring: //消息栏打印警告信息
        AllData = time.toString("hh:mm:ss") + ": <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + Data;
        WarningMessage.push_back(AllData);
        mMessageNumber.WarningMessageNumber = mMessageNumber.WarningMessageNumber + 1;
        this->ui->WarningMessageNumber->setNum(mMessageNumber.WarningMessageNumber);
        break;
    case MessageType::Error: //消息栏打印故障信息
        AllData = time.toString("hh:mm:ss") + ": <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + Data;
        ErrorMessage.push_back(AllData);
        mMessageNumber.ErrorMessageNumber = mMessageNumber.ErrorMessageNumber + 1;
        this->ui->ErrorMessageNumber->setNum(mMessageNumber.ErrorMessageNumber);
        break;
    default:
        break;
    }
    return 1;
}

short HmiThread ::HmiMessageBoxShow(){
    if(ErrorMessage.isEmpty()){
        if(WarningMessage.isEmpty()){
            if(NormalMessage.isEmpty()){
                this->ui->MessageBox->setText(" ");
                this->ui->MessageBox->setStyleSheet("QLabel{border:0px solid rgb(0,0,0);}");
            }
            else{
                this->ui->MessageBox->setText(NormalMessage[NormalMessage.size()-1]);
                this->ui->MessageBox->setStyleSheet("QLabel{border:2px solid rgb(0,0,0);}");
            }
        }
        else{
            this->ui->MessageBox->setText(WarningMessage[WarningMessage.size()-1]);
            this->ui->MessageBox->setStyleSheet("QLabel{border:2px solid rgb(255,255,0);}");
        }
    }
    else{
        this->ui->MessageBox->setText(ErrorMessage[ErrorMessage.size()-1]);
        this->ui->MessageBox->setStyleSheet("QLabel{border:2px solid rgb(255,0,0);}");
    }
    return 1;
}

short HmiThread :: WriteProgressBar(const int value){
    this->ui->progressBar->setValue(value);
    return 1;
}

void  HmiThread :: GetHmiServerHandle(HmiServer* p){
    this->pHmiServerHandle = p;
}
